package com.swm.jdo;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.springframework.stereotype.Repository;

import com.swm.dao.ProjectDao;
import com.swm.model.Movie;
import com.swm.model.Project;
import com.swm.model.Testcase;

@Repository
public class ProjectDaoImpl implements ProjectDao {

	PersistenceManagerFactory pmf;
	
	public ProjectDaoImpl(){
		this.pmf = PMF.get();
	}
	
	protected PersistenceManager getPersistenceManager(){
		return pmf.getPersistenceManager();
	}
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Project> find(){
		PersistenceManager pm = getPersistenceManager();
		List<Project> projects;
		try{
			Query query = pm.newQuery(Project.class);
			projects = (List<Project>) query.execute();
			projects.size();
		} finally{
			pm.close();
		}
		return projects;
	}
	
	@Override
	public Project find(String projectID){
		PersistenceManager pm = getPersistenceManager();
		Project project;
        try {
        	project = pm.getObjectById(Project.class, projectID);
        } finally{
        	pm.close();
        }
        return project;
	}

	@Override
	public void Add(Project project) {
		// TODO Auto-generated method stub
		
		PersistenceManager pm = getPersistenceManager();		
		if ( pm != null ){			
	        try {	        	
	            pm.makePersistent(project);	            
	        } finally {	        	
	            pm.close();
	        }	        
		}		
	}
	
	@Override
	public void Add( Testcase testcase ){
		PersistenceManager pm = getPersistenceManager();	
        try {
            pm.makePersistent(testcase);
        } finally {
            pm.close();
        }
	}

	/*
	@Override
	public void Update( Testcase testcase ) {
		// TODO Auto-generated method stub
		PersistenceManager pm = getPersistenceManager();
		
		try {
			String projectName  = testcase.getProjectName();
			String testcaseName = testcase.getTestcaseName();
			
			Query query = pm.newQuery(Movie.class);
			query.setFilter("projectName == projectParam && testcaseName == testcaseParam");
			query.declareParameters("String projectParam, String testcaseParam");
			List<Testcase> results = (List<Testcase>) query.execute(projectName, testcaseName);
			
			if (results.size() == 1) {
				Testcase oldMovie = results.get(0);
				Long oldMoviePK = oldMovie.getId();
				
				newMovie.setId( oldMoviePK );
				
			    pm.makePersistent(newMovie);
			}
		} finally {
			pm.close();
		}
	}
	*/
	
	@Override
	public void DeleteTestcase(String projectID, String testcaseID){        
        PersistenceManager pm = getPersistenceManager();
        
        try {
			Query query = pm.newQuery( Testcase.class);
			query.setFilter("projectName == projectParam && testcaseName == testcaseParam");
			query.declareParameters("String projectParam, String testcaseParam");
			List<Testcase> results = (List<Testcase>) query.execute( projectID, testcaseID );
			
			if (results.size() == 1) {
				Testcase testcase = results.get(0);				
				pm.deletePersistent(testcase);
			}
		} finally {
			pm.close();
		}
	}

	@Override
	public void Delete(String projectID) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Update(Testcase testcase) {
		// TODO Auto-generated method stub
		
	}		
}
